﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using IceStart.Core;
using System.Windows.Interop;
using System.Windows.Input;
using NHotkey.Wpf;

namespace IceStart.Utils
{
    public class Win32API
    {
        #region 启动调用外部程序

        public static void StartProgram(FileMagnetTie MagnetTie, Window window)
        {
            Window wnd = Window.GetWindow(window);
            IntPtr handle = new WindowInteropHelper(wnd).Handle;
            Win32API.ShellExecute(
                handle, "open", ((FileMagnetTie)MagnetTie).FilePath, "", "C://Users",
                (int)Win32API.ShowWindowCommands.SW_SHOW);
        }

        /// <param name="hwnd">句柄</param>
        /// <param name="lpOperation">指定要进行的操作 "open"</param>
        /// <param name="lpFile">要执行的程序、要浏览的文件夹或者网址</param>
        /// <param name="lpParameters">若lpFile参数是一个可执行程序，则此参数指定命令行参数</param>
        /// <param name="lpDirectory">指定默认目录</param>
        /// <param name="nShowCmd">若lpFile参数是一个可执行程序，则此参数指定程序窗口的初始显示方式(参考ShowWindowCommands)</param>
        /// <returns></returns>
        [DllImport("shell32.dll")]
        private static extern IntPtr ShellExecute(
            IntPtr hwnd,
            string lpOperation,
            string lpFile,
            string lpParameters,
            string lpDirectory,
            int nShowCmd
        );

        public enum ShowWindowCommands : int
        {
            SW_HIDE = 0,
            SW_SHOWNORMAL = 1,
            SW_NORMAL = 1,
            SW_SHOWMINIMIZED = 2,
            SW_SHOWMAXIMIZED = 3,
            SW_MAXIMIZE = 3,
            SW_SHOWNOACTIVATE = 4,
            SW_SHOW = 5,       //显示一个窗口，同时令其进入活动状态
            SW_MINIMIZE = 6,
            SW_SHOWMINNOACTIVE = 7,
            SW_SHOWNA = 8,
            SW_RESTORE = 9,
            SW_SHOWDEFAULT = 10,
            SW_MAX = 10
        }

        #endregion 启动调用外部程序

        #region 获取软件大图标

        [DllImport("User32.dll")]
        private static extern int PrivateExtractIcons(
             string lpszFile, //file name
             int nIconIndex,  //The zero-based index of the first icon to extract.
             int cxIcon,      //The horizontal icon size wanted.
             int cyIcon,      //The vertical icon size wanted.
             IntPtr[] phicon, //(out) A pointer to the returned array of icon handles.
             int[] piconid,   //(out) A pointer to a returned resource identifier.
             int nIcons,      //The number of icons to extract from the file. Only valid when *.exe and *.dll
             int flags        //Specifies flags that control this function.
         );

        [DllImport("User32.dll")]
        private static extern bool DestroyIcon(
             IntPtr hIcon //A handle to the icon to be destroyed. The icon must not be in use.
        );

        public static Icon? GetFileIcon(string FilePath)
        {
            Icon? ico = null;
            //用于接收获取到的图标指针
            IntPtr[] hIcons = new IntPtr[1];
            //对应的图标id
            int[] ids = new int[1];
            //成功获取到的图标个数
            var successCount = PrivateExtractIcons(FilePath, 0, 256, 256, hIcons, ids, 1, 0);
            for (var i = 0; i < successCount; i++)
            {
                if (hIcons[i] == IntPtr.Zero)
                {
                    continue;
                }
                ico = Icon.FromHandle(hIcons[i]);
            }
            return ico;
        }

        #endregion 获取软件大图标

        #region 注册快捷键

        public static void AddGlobalHotkey(EventHandler<NHotkey.HotkeyEventArgs> handler, string key)
        {
            HotkeyManager.Current.AddOrReplace("Close_Open", (Key)Enum.Parse(typeof(Key), key), ModifierKeys.Alt, handler);
        }

        #endregion 注册快捷键
    }
}